package com.clouds.wms.dao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.clouds.wms.bean.Cmsme;
import com.clouds.wms.exception.DatabaseException;

@Repository("cmsmeDao")
public class CmsmeDaoImpl implements CmsmeDao {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="namedParameterJdbcTemplate")
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public List<Cmsme> getAllCmsmeList() throws DatabaseException {
		String sql = "SELECT * FROM cmsme ORDER BY meaa ASC";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().query(sql.toString(), new BeanPropertyRowMapper<Cmsme>(Cmsme.class));
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}

	@Override
	public List<Cmsme> getCmsmeListByMbaa(String mbaa) throws DatabaseException {
		String sql = "SELECT me.meaa,me.meab,me.meac,me.mead,me.meae FROM cmsme me LEFT JOIN cmsmg AS mg ON me.meaa=mg.mgac WHERE mg.mgab=? ORDER BY meaa ASC";
		try {
			return namedParameterJdbcTemplate.getJdbcOperations().query(sql.toString(), new Object[]{mbaa}, new BeanPropertyRowMapper<Cmsme>(Cmsme.class));
		} catch (Exception e) {
			log.error("exception :", e);
			throw new DatabaseException(e.getMessage());
		}
	}

}
